package com.openthinks.joyniproject.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.openthinks.joyniproject.model.Dept;
import com.openthinks.joyniproject.service.DeptService;
import com.openthinks.joyniproject.utils.BaseUtil;

/**
 * 部门 Controller
 * @since 
 *		v1.0
 * @version
 * 		v1.0, 2013-06-27 19:41:40
 * @author 
 *		Cloud
 */
@Controller
@RequestMapping("/dept")
public class DeptController {

	@Autowired
	private DeptService service;
	
	/**
	 * 获取所有部门
	 */
	@RequestMapping(value="/findAll",method=RequestMethod.GET)
    @ResponseBody
	public Map<String, Object> findAll(@RequestParam Map<String,String> data, HttpServletRequest request) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		List<Dept> list = service.findAll();
		
		map.put("list", list);
		return map;
	}
	
	/**
	 * 获取所有部门
	 */
	@RequestMapping(value="/findSubByPid",method=RequestMethod.GET)
    @ResponseBody
	public List<Map<String, String>> findSubByPid(@RequestParam Map<String,String> data, HttpServletRequest request) {
		
		Long pid = BaseUtil.parseLong(data.get("pid"));
		pid = pid == null ? Dept.ROOT_ID : pid;
		List<Dept> list = service.findSubByPid(pid);
		List<Map<String, String>> ret = new ArrayList<Map<String,String>>();
		for (Dept bean : list) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", bean.getId().toString());
			map.put("name", bean.getName());
			map.put("isParent", "true");
			map.put("nocheck", pid == Dept.ROOT_ID ? "true" : "false");
			ret.add(map);
		}
		return ret;
	}
}
